<?php
/**
  * Mar 11, 2008
  *
  * Coded by: Ropu
  *
  */


class Hi5Auth extends Hi5 {
  
  public function __construct($api_key, $uid, $hi5AuthToken=null) {
    parent::__construct($api_key, $uid, $hi5AuthToken);
  }

  public function basic() {
    try {
      return Http::get("http://api.hi5.com/" . $this->type . "/auth/basic", 
	                      array("api_key" => $this->api_key)
	                      ); 
	  } catch (HttpException $e) {
      switch($e->getCode()) {
        case '403':
          throw new PermissionDeniedException($e->getMessage(), '403');
          break;
        default:
          throw new Exception($e->getMessage(), $e->getCode());
          break;
      }
    }
  }

  public function decodeToken() {
    try {
	    return Http::get("http://api.hi5.com/" . $this->type . 
	                                   "/auth/decodetoken/" . $this->Hi5AuthToken,
                        array("Hi5AuthToken" => $this->Hi5AuthToken)
	                      );
	  } catch (HttpException $e) {
      switch($e->getCode()) {
        case '403':
          throw new PermissionDeniedException($e->getMessage(), '403');
          break;
        default:
          throw new Exception($e->getMessage(), $e->getCode());
          break;
      }
    }
  }
  
  public function plain($username, $password) {
    try {
	    return Http::post("http://api.hi5.com/" . $this->type . "/auth/plain/" . 
	                                                            $this->uid,
//	    return Http::post("http://localhost:8888/test/post.php",
                        array("api_key" => $this->api_key,
	                            "username" => $username,
	                            "password" => $password)
	                      );
	  } catch (HttpException $e) {
      switch($e->getCode()) {
        case '403':
          throw new PermissionDeniedException($e->getMessage(), '403');
          break;
        default:
          throw new Exception($e->getMessage(), $e->getCode());
          break;
      }
    }
  }
  
  public function renew() {
    /*
     * TODO: review
     */
    try {
	    return Http::post("http://api.hi5.com/" . $this->type . "/auth/renew",
	                      array("api_key" => $this->api_key,
	                            "Hi5AuthToken" => $this->Hi5AuthToken)
	                      );
    } catch (HttpException $e) {
      switch($e->getCode()) {
        case '403':
          throw new PermissionDeniedException($e->getMessage(), '403');
          break;
        default:
          throw new Exception($e->getMessage(), $e->getCode());
          break;
      }
    }
  }
  
  public function wsse() {
    /*
     * TODO:implement
     */
    "http://api.hi5.com/" . $this->type . "/auth/wsse";
  }
  
  public function validateSession() {
    try {
	    $token = new SimpleXMLElement($this->decodeToken(), null, null, 'hi5', true);
	    return ((string)$token->attributes()->userId==$this->uid)?true:false;
    } catch(Exception $e) {
      return false;
    }
  }
   
}

?>